#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_logcolllector.py 
@version:
@time: 2022/05/17 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM
from TapException.TM import LogcollectorNotFound


class TMFLogCollector:

    # 重置共享挖掘任务
    @staticmethod
    def _patch_task_renew(task_id):
        return TM.patch_task_renew(task_id)

    @staticmethod
    def _patch_task_start(task_id):
        return TM.put_task_start_by_id(task_id)

    # 获取共享挖掘全局配置
    @staticmethod
    def _get_collector_system_config():
        return TM.get_collector_system_config().get('data')

    # 根据filter获取logcollector
    @staticmethod
    def _get_logcollector_by_filter(filter_):
        return TM.get_logcollector_by_filter(filter_).get('data')

    # 根据id获取logcollector细节
    @staticmethod
    def _get_logcollector_detail_by_id(logcollector_id):
        return TM.get_logcollector_detail_by_id(logcollector_id).get('data')

    # 重置
    def renew_logcollector_by_name(self, connection_name):
        task_id = self.get_logcollector_detail_by_connection_name(connection_name).get('id')
        return self._patch_task_renew(task_id)

    # start
    def start_logcollector_by_name(self, connection_name):
        task_id = self.get_logcollector_detail_by_connection_name(connection_name).get('id')
        return self._patch_task_start(task_id)

    # 通过所属链接获取 共享挖掘的 记录
    def get_logcollector_by_connection_name(self, connection_name):
        limit = 20
        skip = 0
        while True:
            _filter = {
                "order": "createTime DESC",
                "limit": limit,
                "skip": skip,
                "where": {}
            }
            response = self._get_logcollector_by_filter(_filter)
            items = response.get('items')
            if not items:
                break
            else:
                for one in items:
                    # 去除 - 来自。。。的共享挖掘任务
                    if one.get('name')[2:-7] == connection_name:
                        return one
            skip += limit

    # 根据所属链接获取 共享挖掘的 细节
    def get_logcollector_detail_by_connection_name(self, connection_name):
        record_id = self.get_logcollector_by_connection_name(connection_name).get('id')
        return self._get_logcollector_detail_by_id(record_id)

    # 通过 链接名 任务名 获取单个任务共享挖掘信息
    def get_logminer_by_connection_name_and_task_name(self, connection_name, task_name):
        task_list = self.get_logcollector_detail_by_connection_name(connection_name).get('taskList')
        for one in task_list:
            if one.get('name')[:-4] == task_name:
                return one
        raise LogcollectorNotFound

    # 获取共享挖掘全局配置需要的参数
    def get_logcollector_attr(self):
        response = self._get_collector_system_config()
        persistenceMongodb_uri_db = response.get('persistenceMongodb_uri_db')
        persistenceMongodb_collection = response.get('persistenceMongodb_collection')
        share_cdc_ttl_day = response.get('share_cdc_ttl_day')
        return {
            'shareCdcEnable': True,
            'persistenceMongodb_uri_db': persistenceMongodb_uri_db,
            'persistenceMongodb_collection': persistenceMongodb_collection,
            'share_cdc_ttl_day': share_cdc_ttl_day
        }
